//
val bools = List(true,false,Nil)
for (bool <- bools){
  bool match {
    case true => println("heads")
    case false => println("tails")
    case _ => println("null")
  }
}

bools.foreach {
  case true => println("heads")
  case false => println("tails")
  case _ => println("null")
}

import scala.util.Random
val randomInt = new Random().nextInt(10)
randomInt match {
  case 7 => println("lucky seven!")
  case otherNumber => println(otherNumber)
}

val sundries = List(23,"Hello",8.5,'q')
sundries.foreach{
  case _:Int => println("int")
  case  _:String => println("String")
  case  _:Double => println("double")
  case other => println("other")
}

val willWork = List(1,3,23,90)
val willNotWork = List(4,18,53)
val empty = List()
for (l <- List(willNotWork,willWork,empty)){
  l match {
    case List(_,3,_,_) => println("four element and 2nd is 3")
    case List(_*) => println("Any other list with 0 or more elements.")
  }
}

val tupA = ("Good", "Morning!")
val tupB = ("Guten", "Tag!")
for (tup <- List(tupA,tupB)) {
  tup match {
    case (thingOne,thingTwo) if thingOne == "Good" =>
      println("A two-tuple starting with 'Good'.")
    case (thingOne,thingTwo) =>
      println("This has two thing:" + thingOne + " and " + thingTwo)
  }
}

//
case class Person(name: String, age: Int)
val alice = new Person("alice",22)
val bob = new Person("bob", 23)
val charlie = new Person("Charlie", 32)
for (person <- List(alice, bob, charlie)){
  person match {
    case Person("alice",22) => println("Hi,Alice!")
    case Person("bob",23) => println("Hi bob!")
    case Person(name,age) => println("who are you ," + name + "")
  }
}

val  BookExtractorRE = """Book: title=([^,]+),\s+authors=(.+)""".r
val MagazineExtractorRE = """Magazine: title=([^,]+),\s+issue=(.+)""".r
val catalog = List(
  "Book: title=Programming Scala, authors=Dean Wampler, Alex Payne",
  "Magazine: title=The New Yorker, issue=January 2009",
  "Book: title=War and Peace, authors=Leo Tolstoy",
  "Magazine: title=The Atlantic, issue=February 2009",
  "BadData: text=Who put this here??"
)
for (item <- catalog) {
  item match {
    case BookExtractorRE(title, authors) =>
      println("Book \"" + title + "\", written by " + authors)
    case MagazineExtractorRE(title, issue) =>
      println("Magazine \"" + title + "\", issue " + issue)
    case entry => println("Unrecognized entry: " + entry)
  }
}